<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="直播课名称" prop="teachcastName" label-width="100px">
        <el-input
          v-model="queryParams.teachcastName"
          placeholder="请输入直播课名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="直播教师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入直播教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="直播时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:teachcast:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:manHour:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:manHour:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->
    <el-table v-loading="loading" :data="manHourList" @selection-change="handleSelectionChange" @row-dblclick="handleRowDoubleClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column header-align="center" align="center" prop="channelid" label="频道号" width="100" />
      <el-table-column prop="teachcastName" header-align="left" align="left" label="直播课名称" :show-overflow-tooltip="true" />
      <el-table-column prop="teachcastContent" header-align="left" align="left" label="直播内容" :show-overflow-tooltip="true" />
      <el-table-column header-align="center" align="center" prop="teacherName" label="直播教师" />
      <el-table-column header-align="center" align="center" prop="teachcastTime" label="直播时间" />
      <el-table-column header-align="left" align="left" prop="teachcastDepcName" label="直播对象" :show-overflow-tooltip="true" />
      <el-table-column header-align="left" align="left" prop="state" label="直播状态" :show-overflow-tooltip="true" />
      <el-table-column header-align="center" align="center" prop="id" label="操作" width="350">
          <template slot-scope="scope">
            <el-button  v-if="scope.row.state=='未开始'" @click="openlivestreaming(scope.row)" type="primary" round size="mini">开启直播</el-button>
            <!--          <el-button @click="watchlivestreaming(scope.row)" type="primary" round size="mini">观看直播</el-button>-->
            <!--          <el-button @click="viewOverlaid(scope.row)" type="primary" round size="mini">观看回放</el-button>-->
            <!--          <el-button type="primary" round size="mini">下载</el-button>-->
            <el-button v-if="scope.row.state=='未开始'" @click="copylink(scope.row)" type="primary" round size="mini">复制链接</el-button>
          </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改日精进打卡对话框 -->
    <el-drawer :withHeader="true" :title="title" :visible.sync="open" :with-header="false" size="30%"></el-drawer>
  </div>
</template>

<script>
import {getManHour, queryHourByTime} from "@/api/system/mis/manHour";
import {delManHourDetail, getManHourDetail, listManHourDetail} from "@/api/system/mis/manHourDetail";
import moment from "moment";
import {getMethod, postMethod} from "@/api/system/livelesson/quote";

export default {
  name: "ManHour",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工时填报表格数据
      manHourList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryTime: undefined,
      hourLoading:false,
      hourData:[],
      dateOne: [],
      dateRange: [],
      content: '今日未填写人数：',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptCode: undefined,
        teachcastName: undefined,
        projectId: undefined,
        projectCode: undefined,
        teacherName: undefined,
        questId: undefined,
        projectManager: undefined,
        projectManagerCode: undefined,
        isOvertime: undefined,
        projectDept: undefined,
        projectDeptCode: undefined,
        createByName: undefined,
        manHour: undefined,
        predictManHour: undefined,
        reportingContent: undefined,
        content: undefined,
        state: undefined,
        generatedState: undefined,
      },
      form:{}
    };
  },
  created() {
    this.queryTime = new Date();
    this.queryTime = moment().format('YYYY-MM-DD HH:mm:ss');
    this.getList();

  },
  methods: {
    // 开启直播
    openlivestreaming(val) {
      console.log(val.channelid)
      console.log(val)
      postMethod("/livelesson/teachcast/queryToken", val).then((response) => {
        val.token = response.dataset.datas.token;
        this.$message({
          type: "success",
          message: "请求成功!"
        });
        this.$router.push({
          path: "/livelesson/livelessoninfo",
          query: {
            formData: val,
            isView: 'add'
          },
        });
      });
    },
    // 观看直播
    watchlivestreaming(val) {
      console.log(val)
      this.$router.push({
        path: "/livelesson/livelessoninfo",
        query: {
          formData: val,
          isView: 'add'
        },
      });
    },
    // 复制链接
    copylink(val) {
      console.log(val)
      let url = 'https://live.polyv.cn/watch/' + val.channelid
      let textarea = document.createElement("textarea")
      // 设置textarea的值为要复制的文本
      textarea.value = url;
      // 将textarea元素添加到DOM中
      document.body.appendChild(textarea)
      // 选中textarea中的文本
      textarea.select();
      // 复制到剪切板
      document.execCommand("copy");
      // 删除textarea元素
      document.body.removeChild(textarea);
      this.$modal.msgSuccess("复制成功");

    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.teacherCode = this.$store.state.user.name
      // this.queryByTime()
      getMethod("/livelesson/teachcast/list",this.addDateRange(this.queryParams,this.dateRange)).then(response => {
        this.manHourList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange =[]
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.dateOne = selection
      this.ids = selection.map(item => item.id)
      this.idSons = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/livelesson/teachcastForm",
        query: {
          isView: 'add'
        },
      });
    },
    // 双击列表
    handleRowDoubleClick(row) {
      debugger
      const id = row.id || this.ids
      getManHourDetail(id).then(response => {
        this.open = true;
        this.loading = false;
        this.form = response.data;
        this.title = "查看工时详情";
      });
    },
    queryTimeChange(row){
      if (row !== null){
        // this.queryByTime()
      }
    },
    /** 查询未填写人数 */
    handleLinkClick(row){
      this.$router.push({
        path: "/everyWork/manHourForm",
        query: { isView: 'view',id: row.refId},
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (this.dateOne[0].teacherCode !== this.$store.state.user.name){
        return this.$message.warning("只能修改自己创建的直播课")
      }
      if (this.ids.length > 1){
        return this.$message.warning("请选择一条记录")
      }
      const id = this.ids[0]
      this.$router.push({
        path: "/livelesson/teachcastForm",
        query: {
          id: id,
          isView: 'edit'
        },
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.idSons;
      console.log(ids)
      this.$modal.confirm('是否确认删除数据？').then(() => {
        this.loading = true;
        return delMethod("/livelesson/teachcast/" + ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/manHour/export', {
        ...this.queryParams
      }, `工时填报.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
